﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.BL.Entities;
using QUANLYCHAMCONG.BL.BUS;
using zkemkeeper;
using Hopthoai;
using Microsoft.VisualBasic.CompilerServices;
using System.Runtime.CompilerServices;
using Microsoft.VisualBasic;
using QUANLYCHAMCONG.BL;
using QUANLYCHAMCONG.BL.BUS.Import;
using zkemkeeper;

namespace QUANLYCHAMCONG.PL.CMS.Maychamcong
{
    public partial class Upnhanvientoimaychamcong : UserControl
    {
        int sonhanvien = 0;
        string mamay = "";
        string seri = "";
        bool IsResignter = true;
        string serimahoa = "";
        int flag = 0;
        int UserCount=0;
        int FPCount = 0;
        public delegate void _Close( );
        public event _Close Close;
        DataTable dauvantay;
        MAYCHAMCONGCollection dsmaychamcong= new MAYCHAMCONGCollection();
        public Upnhanvientoimaychamcong()
        {
            InitializeComponent();
        }
        bool IsConnect;
        public CZKEM AxCZKEM1 = new CZKEMClass();
        frmHoidap thongbao;
        MAYCHAMCONG mcc;
        public Total UcStatusRoom { get; set; }
        public void ReloadStattus()
        {
            Thongke tk = new Thongke();
            this.UcStatusRoom.DtData = BAOCAOBUS.ThongkeTotal(tk);
            this.UcStatusRoom.Settext();
        }
        private void cmbcongty_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbcongty.SelectedIndex != -1 && this.cmbcongty.SelectedValue.ToString() != "System.Data.DataRowView")
            { 
                //doc thong tin ve cong ty
                PHONGBAN pb = new PHONGBAN();
                pb.MACTY = this.cmbcongty.SelectedValue.ToString();
                DataTable dt = new DataTable();
                dt = Common.InsertALL(PHONGBANBUS.SelectBYCTY(pb), "TENPB", "MAPB");
                cmbphongban.DataSource = dt;
                cmbphongban.DisplayMember = "TENPB";
                cmbphongban.ValueMember = "MAPB";
                cmbphongban.Enabled = true;
                cmbchucvu.Enabled = true;
            }
        }
        void LoadAllMAY()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = MAYCHAMCONGBUS.SelectAll(null);
                this.toolbarMayChamCong1.DataLoaiMay = dt;
                this.toolbarMayChamCong1.DisplayMember = "TENMAY";
                this.toolbarMayChamCong1.ValuesMember = "MAMCC";
                this.toolbarMayChamCong1.AddDataForCMB();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi :" + ex.Message.ToString());
            }
           
        }
        private void SetToolbar()
        {
            this.toolbarMayChamCong1.SetEnableButton(false, true, true, true, false, true, true, true, true);
        }
        void LoadCombobox()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = Common.InsertALL(CHUCVUBUS.SelectAll(), "TENCV", "MACV");
                this.cmbchucvu.DataSource = dt;
                this.cmbchucvu.DisplayMember = "TENCV";
                this.cmbchucvu.ValueMember = "MACV";
                DataTable congty = new DataTable();
                this.cmbcongty.DataSource = CONGTYBUS.SelectAll();
                this.cmbcongty.DisplayMember = "TENDKY";
                this.cmbcongty.ValueMember = "MACTY";
            }
            catch(Exception ex)
            {
                MessageBox.Show("Lỗi :" + ex.Message.ToString());
            }
        }
        private void Upnhanvientoimaychamcong_Load(object sender, EventArgs e)
        {
            LoadCombobox();
            SetLink();
            LoadAllMAY();
            SetToolbar();
        }
        private void SetLink()
        {
            this.links1.SetValues("Máy", "Upload Nhân viên tới máy chấm công");
        }
        private void toolbarMayChamCong1_ChangeMachine()
        {
            mamay = this.toolbarMayChamCong1.SelectValue;
        }
        private void FindStaff(string mapb, string macv)
        {
            STAFF nv = new STAFF();
            nv.MAPB = mapb;
            nv.MACV = macv;
        }
        private void cmbphongban_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbphongban.SelectedValue.ToString() != "System.Data.DataRowView")
            {
                STAFF nv = new STAFF();
                nv.MAPB = this.cmbphongban.SelectedValue.ToString();
                nv.MACV = this.cmbchucvu.SelectedValue.ToString();
                dgvdanhsachnv.DataSource = STAFFBUS.SelectFollowPB(nv);
            }
        }
        private void cmbchucvu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbchucvu.SelectedValue.ToString() != "System.Data.DataRowView")
            {
                STAFF nv = new STAFF();
                nv.MAPB = this.cmbphongban.SelectedValue.ToString();
                nv.MACV = this.cmbchucvu.SelectedValue.ToString();
                dgvdanhsachnv.DataSource = STAFFBUS.SelectFollowPB(nv);
            }
        }
        private int FindAviable()
        {
            foreach (DataGridViewColumn dr in this.dgvdanhsachnv.Columns)
            {
                if (dr is DataGridViewCheckBoxColumn)
                    return 1;
            }
            return 0;
        }
        private void RemoveCheckbox()
        {
            if (FindAviable() == 1)
            {
                this.dgvdanhsachnv.Columns.RemoveAt(0);
                this.dgvdanhsachnv.Controls.RemoveByKey(((CheckBox)this.dgvdanhsachnv.Controls.Find("checkboxHeader", true)[0]).Name);
               
            }
        }
        private void AddCheckbox(DataTable dt)
        {
            if (FindAviable() == 0)
            {
                DataGridViewCheckBoxColumn cb = new DataGridViewCheckBoxColumn();
                cb.Width = 30;
                cb.FillWeight = 30;
                cb.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                this.dgvdanhsachnv.Columns.Insert(0, cb);
                Rectangle rect = this.dgvdanhsachnv.GetCellDisplayRectangle(0, -1, true);
                rect.X = rect.Location.X + cb.Width / 10;
                CheckBox checkboxHeader = new CheckBox();
                checkboxHeader.Name = "checkboxHeader";
                checkboxHeader.Size = new Size(18, 18);
                checkboxHeader.Location = rect.Location;
                checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
                this.dgvdanhsachnv.Controls.Add(checkboxHeader);
                this.dgvdanhsachnv.Columns[0].Width = 50;
                this.dgvdanhsachnv.DataSource = dt;
            }
        }
        void checkboxHeader_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)this.dgvdanhsachnv.Controls.Find("checkboxHeader", true)[0]).Checked == true)
            {
                for (int i = 0; i < this.dgvdanhsachnv.RowCount; i++)
                {
                    this.dgvdanhsachnv[0, i].Value = ((CheckBox)this.dgvdanhsachnv.Controls.Find("checkboxHeader", true)[0]).Checked;
                }
            }
            else
            {
                for (int i = 0; i < this.dgvdanhsachnv.RowCount; i++)
                {
                    this.dgvdanhsachnv[0, i].Value = false;
                }
            }
        }
        private void toolbarMayChamCong1_Upload()
        {
            //deactive every toolbar
            if (cmbphongban.Enabled == true && cmbchucvu.Enabled == true)
            {
                this.toolbarMayChamCong1.SetEnableButton(false, false, false, false, true, false, false, false, true);
                if (this.cmbphongban.SelectedValue.ToString() != "System.Data.DataRowView" && this.cmbchucvu.SelectedValue.ToString() != "System.Data.DataRowView")
                {
                    STAFF nv = new STAFF();
                    nv.MAPB = this.cmbphongban.SelectedValue.ToString();
                    nv.MACV = this.cmbchucvu.SelectedValue.ToString();
                    AddCheckbox(STAFFBUS.SelectFollowPB(nv));
                    //sau khi chon du lieu 
                    flag = 0;
                }
            }
        }
        void CreateConection(MAYCHAMCONG mcc)
        {
            if (mcc.MALKN == "COM")
            {
                Connection(ConnectType.COM, mcc.ISDOMAIN, mcc);
            }
            else if (mcc.MALKN == "TCP")
            {
                Connection(ConnectType.TCP, mcc.ISDOMAIN, mcc);
            }
            else if (mcc.MALKN == "USB")
            {
                Connection(ConnectType.USB, mcc.ISDOMAIN, mcc);
            }
        }
        bool CheckSeriAl(string serial)
        {
            mahoa.mahoa mh = new mahoa.mahoa();
            serimahoa = mh.mahoadulieu(seri, false, "thanhthuy");
            if (!Common.IsExists(Application.StartupPath + @"\" + serimahoa + ".GDT"))
            {
                IsResignter = false;
                SetToolbar();
                thongbao = new frmHoidap("Máy không đúng hoặc chưa được đăng ký \n vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                thongbao.ShowDialog();
                return false;
            }
            else
            {
                return true;
            }
        }
        private void UpLoadOneMachine()
        {
            try
            {
                if( !String.IsNullOrEmpty(mamay) )
                {
                    if( mamay.Trim() == "-1" )
                    {
                        thongbao = new frmHoidap("Bạn vui lòng chọn máy cụ thể!" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.canhbao);
                        thongbao.ShowDialog();
                    }
                    else
                    {
                        UserCount = 0;
                        DataTable dt = new DataTable();
                        mcc = new MAYCHAMCONG();
                        mcc.MAMCC = mamay;
                        dt = MAYCHAMCONGBUS.SelectPK(mcc);
                        if( dt.Rows.Count != 0 )
                        {
                            mcc = new MAYCHAMCONG();
                            DataTableToMaychamcong(ref mcc , dt);
                            frmwaiting f = new frmwaiting();
                            f.StartPosition = FormStartPosition.CenterScreen;
                            f.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                            f.Show();
                            Application.DoEvents();
                            CreateConection(mcc);
                            if( IsConnect )
                            {
                                AxCZKEM1.GetSerialNumber(mcc.IDMAY , out seri);
                                if( !CheckSeriAl(seri) )
                                {
                                    f.Close();
                                    SetToolbar();
                                    AxCZKEM1.Disconnect();
                                }
                                else
                                {
                                    //neu ket noi thanh cong 
                                    dauvantay = new DataTable();
                                    int count = dgvdanhsachnv.Rows.Count - 1;
                                    AxCZKEM1.BASE64 = 0;
                                    if( dgvdanhsachnv.Rows.Count != 0 )//neu bang danh sach nhan vien co du lieu
                                    {

                                        f.tocdo.Maximum = count;
                                        f.tocdo.Visible = true;
                                        f.lbgoi.Text = "0%";
                                        f.lbgoi.Text = "Đang tải nhân viên lên máy " + mcc.IDMAY.ToString();
                                        Application.DoEvents();
                                        //----------------------------------------------------------------------------------
                                        AxCZKEM1.EnableDevice(mcc.IDMAY , false);
                                        UserCount = 0;
                                        TemplateCololection dsvantay = new TemplateCololection();
                                        for( int i = 0 ; i <= count ; i++ )
                                        {
                                            int FingerID = 0;
                                            string tmpData = "";
                                            int length = 0;
                                            DataGridView dgv = this.dgvdanhsachnv;
                                            if( dgv.Rows [i].Cells [0].Value != null )
                                            {
                                                int dwEnrollNumber;
                                                if( dgv.Rows [i].Cells ["MACCM"].Value.ToString() == "" )
                                                    continue;
                                                else
                                                    dwEnrollNumber = Conversions.ToInteger(dgv.Rows [i].Cells ["MACCM"].Value.ToString());
                                                string TENCC = dgv.Rows [i].Cells ["TENCC"].Value.ToString() ;
                                                string UserCardNo =dgv.Rows [i].Cells ["UserCardNo"].Value.ToString();
                                                string UserPW = dgv.Rows [i].Cells ["UserPW"].Value.ToString();
                                                int UserPrivilege=0;
                                                if(dgv.Rows [i].Cells ["UserPrivilege"].Value.ToString()=="")
                                                    UserPrivilege=0;
                                                else
                                                    UserPrivilege= int.Parse(dgv.Rows [i].Cells ["UserPrivilege"].Value.ToString());
                                                bool UserEnabled;
                                                if( dgv.Rows [i].Cells ["UserEnabled"].Value.ToString() == "WORK" )
                                                    UserEnabled = true;
                                                else
                                                    UserEnabled = false;
                                                dgv = null;
                                                //vua sua o dayv
                                                if(!AxCZKEM1.IsTFTMachine(mcc.IDMAY) )
                                                {
                                                    AxCZKEM1.SetStrCardNumber(UserCardNo);//set card number
                                                    if (AxCZKEM1.SetUserInfo(mcc.IDMAY, dwEnrollNumber, TENCC, UserPW, UserPrivilege, UserEnabled))
                                                    {
                                                        UserCount++;
                                                        if (cbvantay.Checked == true)
                                                        {
                                                            //lay tat ca van tay ve 
                                                            dauvantay.Clear();
                                                            Template t = new Template();
                                                            t.MACCM = dgv.Rows[i].Cells["MACCM"].Value.ToString();
                                                            dauvantay = TemplateBUS.SelectPK(t);
                                                            if (dauvantay.Rows.Count != 0)
                                                            {
                                                                TemplateCololection ds = new TemplateCololection();
                                                                DataTableToTemplate(ref ds, dauvantay);//chuyen tu dang table sang dang van tay
                                                                foreach (Template temps in ds)
                                                                {
                                                                    FingerID = temps.FingerID;
                                                                    tmpData = temps.FingerTemplate;
                                                                    if (AxCZKEM1.SetUserTmpStr(mcc.IDMAY, dwEnrollNumber, FingerID, tmpData))
                                                                        this.FPCount++;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    
                                                }
                                                else//phai may tft
                                                {
                                                    string temp=dwEnrollNumber.ToString();
                                                    AxCZKEM1.SetStrCardNumber(UserCardNo);
                                                    //upload luon ca thong tin
                                                    if (AxCZKEM1.SSR_SetUserInfo(mcc.IDMAY, temp, TENCC, UserPW, UserPrivilege, UserEnabled))
                                                    {
                                                        //thuc hien up luvon van tay
                                                        if (cbvantay.Checked == true)
                                                        {
                                                            //dauvantay.Clear();
                                                            Template t = new Template();
                                                            t.MACCM = temp;
                                                            dauvantay = TemplateBUS.SelectPK(t);
                                                            if (dauvantay.Rows.Count != 0)
                                                            {
                                                                TemplateCololection ds = new TemplateCololection();
                                                                DataTableToTemplate(ref ds, dauvantay);
                                                                foreach (Template temps in ds)
                                                                {
                                                                    string sdwEnrollNumber = temp;
                                                                    int idwFingerIndex = temps.FingerID;
                                                                    string sTmpData = temps.FingerTemplate;
                                                                    int flag = 0;
                                                                    //thuc hien uplen may
                                                                    if (AxCZKEM1.SetUserTmpExStr(mcc.IDMAY, sdwEnrollNumber, idwFingerIndex,flag, sTmpData))
                                                                    {
                                                                        this.FPCount++;
                                                                    }
                                                                    if (AxCZKEM1.SetUserFaceStr(mcc.IDMAY, sdwEnrollNumber, idwFingerIndex, tmpData, length))
                                                                    {
                                                                        this.FPCount++;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        UserCount++;
                                                    }
                                                    
                                                }
                                                // tang bien len 1 don vi
                                                f.tocdo.Value = i;
                                                f.lbgoi.Text = Conversions.ToString((double)( Math.Round((double)( ( (double)( i + 1 ) ) / ( (double)count ) ) , 2) * 100.0 )) + "%";
                                                Application.DoEvents();
                                            }
                                        }
                                        f.Close();
                                        //het vong lap
                                        AxCZKEM1.EnableDevice(mcc.IDMAY , true);
                                        AxCZKEM1.Disconnect();
                                        
                                        thongbao = new frmHoidap("Số nhân viên đã được tải lên máy " + mcc.IDMAY.ToString() + "là :" + UserCount.ToString() + "\r\n Số vân tay được tải lên máy " + mcc.IDMAY.ToString() + "là :" + FPCount.ToString() , frmHoidap.CO.hien , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.thongtin);
                                        thongbao.ShowDialog();
                                        SetToolbar();
                                    }
                                }
                            }
                            else
                            {
                                f.Close();
                                f.Dispose();
                                SetToolbar();
                                thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.baoloi);
                                thongbao.ShowDialog();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi :" + ex.Message.ToString());
            }
        }
        private void UpLoadALLMachine()
        {
            if (!String.IsNullOrEmpty(mamay))
            {
                try
                {
                    int tongsomay = 0;
                 
                    tongsomay = Tongsomay();
                    DataTable danhsachmay = MAYCHAMCONGBUS.SelectAll(null);
                    ConvertData(danhsachmay);
                    int FingerID = 0;
                    TemplateCololection dsvantay = new TemplateCololection();
                    DataTable dauvantay=new DataTable();
                    foreach (MAYCHAMCONG cc in dsmaychamcong)
                    {
                        this.UserCount = 0;
                        this.FPCount = 0;
                        frmwaiting f = new frmwaiting();
                        f.StartPosition = FormStartPosition.CenterScreen;
                        f.lbgoi.Text = "Đang gọi máy " + cc.IDMAY.ToString() + "......";
                        f.Show();
                        Application.DoEvents();
                        //tao ket noi
                        CreateConection(mcc);
                        if (IsConnect)
                        {
                            AxCZKEM1.BASE64 = 0;
                            AxCZKEM1.GetSerialNumber(mcc.IDMAY, out seri);
                            if( !CheckSeriAl(seri) )
                            {
                                f.Close();
                                SetToolbar();
                                AxCZKEM1.Disconnect();
                            }
                            else
                            {
                                //khi ket noi thanh cong may
                                int count = dgvdanhsachnv.Rows.Count-1;
                                if (dgvdanhsachnv.Rows.Count != 0)//neu bang danh sach nhan vien co du lieu
                                {
                                    
                                    f.tocdo.Maximum = count;
                                    f.tocdo.Visible = true;
                                    f.lbgoi.Text = "0%";
                                    f.lbgoi.Text = "Đang tải nhân viên lên máy" + mcc.IDMAY.ToString();
                                    Application.DoEvents();
                                    AxCZKEM1.EnableDevice(mcc.IDMAY, false);
                                    for (int i = 0; i <= count; i++)
                                    {
                                        string tmpData = "";
                                        int length = 0;
                                        DataGridView dgv = this.dgvdanhsachnv;
                                        if (dgv.Rows[i].Cells[0].Value != null)
                                        {
                                            int dwEnrollNumber;
                                            if (dgv.Rows[i].Cells["MACCM"].Value.ToString() == "")
                                                continue;
                                            else
                                                dwEnrollNumber = Conversions.ToInteger(dgv.Rows[i].Cells["MACCM"].Value.ToString());
                                            string TENCC = dgv.Rows[i].Cells["TENCC"].Value.ToString();
                                            string UserCardNo = dgv.Rows[i].Cells["UserCardNo"].Value.ToString();
                                            string UserPW = dgv.Rows[i].Cells["UserPW"].Value.ToString();
                                            int UserPrivilege = 0;
                                            if (dgv.Rows[i].Cells["UserPrivilege"].Value.ToString() == "")
                                                UserPrivilege = 0;
                                            else
                                                UserPrivilege = int.Parse(dgv.Rows[i].Cells["UserPrivilege"].Value.ToString());
                                            bool UserEnabled;
                                            if (dgv.Rows[i].Cells["UserEnabled"].Value.ToString() == "WORK")
                                                UserEnabled = true;
                                            else
                                                UserEnabled = false;
                                            dgv = null;
                                            if (!AxCZKEM1.IsTFTMachine(mcc.IDMAY))
                                            {
                                                AxCZKEM1.SetStrCardNumber(UserCardNo);//set card number
                                                if (AxCZKEM1.SetUserInfo(mcc.IDMAY, dwEnrollNumber, TENCC, UserPW, UserPrivilege, UserEnabled))
                                                {
                                                    UserCount++;
                                                    if (cbvantay.Checked == true)
                                                    {
                                                        //lay tat ca van tay ve 
                                                        dauvantay.Clear();
                                                        Template t = new Template();
                                                        t.MACCM = dwEnrollNumber.ToString();
                                                        dauvantay = TemplateBUS.SelectPK(t);
                                                        if (dauvantay.Rows.Count != 0)
                                                        {
                                                            TemplateCololection vt = new TemplateCololection();
                                                            DataTableToTemplate(ref vt, dauvantay);//chuyen tu dang table sang dang van tay
                                                            foreach (Template temps in vt)
                                                            {
                                                                FingerID = temps.FingerID;
                                                                tmpData = temps.FingerTemplate;
                                                                if (AxCZKEM1.SetUserTmpStr(mcc.IDMAY, dwEnrollNumber, FingerID, tmpData))
                                                                    this.FPCount++;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            else//phai may tft
                                            {
                                                string temp = dwEnrollNumber.ToString();
                                                AxCZKEM1.SetStrCardNumber(UserCardNo);
                                                //upload luon ca thong tin
                                                if (AxCZKEM1.SSR_SetUserInfo(mcc.IDMAY, temp, TENCC, UserPW, UserPrivilege, UserEnabled))
                                                {
                                                    //thuc hien up luvon van tay
                                                    if (cbvantay.Checked == true)
                                                    {
                                                        //dauvantay.Clear();
                                                        Template t = new Template();
                                                        t.MACCM = temp;
                                                        dauvantay = TemplateBUS.SelectPK(t);
                                                        if (dauvantay.Rows.Count != 0)
                                                        {
                                                            TemplateCololection ds = new TemplateCololection();
                                                            DataTableToTemplate(ref ds, dauvantay);
                                                            foreach (Template temps in ds)
                                                            {
                                                                string sdwEnrollNumber = temp;
                                                                int idwFingerIndex = temps.FingerID;
                                                                string sTmpData = temps.FingerTemplate;
                                                                int flag = 0;
                                                                //thuc hien uplen may
                                                                if (AxCZKEM1.SetUserTmpExStr(mcc.IDMAY, sdwEnrollNumber, idwFingerIndex, flag, sTmpData))
                                                                {
                                                                    this.FPCount++;
                                                                }
                                                                if (AxCZKEM1.SetUserFaceStr(mcc.IDMAY, sdwEnrollNumber, idwFingerIndex, tmpData, length))
                                                                {
                                                                    this.FPCount++;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    UserCount++;
                                                }

                                            }
                                            // tang bien len 1 don vi
                                            f.tocdo.Value = i;
                                            f.lbgoi.Text = Conversions.ToString((double)(Math.Round((double)(((double)(i + 1)) / ((double)count)), 2) * 100.0)) + "%";
                                            Application.DoEvents();
                                        }
                                    }
                                    //het vong lap
                                    AxCZKEM1.EnableDevice(mcc.IDMAY, true);
                                    AxCZKEM1.Disconnect();
                                    f.Close();
                                    thongbao = new frmHoidap("Số nhân viên đã được tải lên máy " + mcc.IDMAY.ToString() + "là " + UserCount.ToString() + "\r\n Số vân tay được tải lên máy " + mcc.IDMAY.ToString() + "Là" + FPCount.ToString(), frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                                    thongbao.ShowDialog();
                                }
                            }
                        }
                        else
                        {
                            f.Close();
                            thongbao = new frmHoidap("Không thể mở kết nối đến " + cc.IDMAY.ToString() + "vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            thongbao.ShowDialog();
                        }
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Có lỗi :" + ex.Message.ToString());
                }
            }
        }
        private void DataTableToTemplate(ref TemplateCololection dstemp, DataTable dt)
        {
            try
            {
                if (dt.Rows.Count != 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        Template temp = new Template();
                        temp.MACCM = dr["MACCM"].ToString();
                        temp.MAVT = dr["MAVT"].ToString();
                        temp.TempLength = int.Parse(dr["TempLength"].ToString());
                        temp.FingerID = int.Parse(dr["FingerID"].ToString());
                        temp.FingerTemplate = dr["FingerTemplate"].ToString();
                        dstemp.Add(temp);
                    }
                }
            }
            catch (Exception ex)
            { 
                MessageBox.Show("Lỗi :"+ex.Message.ToString());
            }
           
        }
        private void toolbarMayChamCong1_Save()
        {
            try
            {


                if( !String.IsNullOrEmpty(mamay) )
                {
                    if( flag == 0 && this.dgvdanhsachnv.Rows.Count != 0 )
                    {
                        // thiet lap ket noi toi may cham cong
                        if( mamay == "-1" )
                        {
                            UpLoadALLMachine();
                            SetToolbar();
                            ReloadStattus();
                        }
                        else
                        {
                            ReloadStattus();
                            UpLoadOneMachine();
                        }
                        RemoveCheckbox();
                        STAFF nv = new STAFF();
                        nv.MAPB = this.cmbphongban.SelectedValue.ToString();
                        nv.MACV = this.cmbchucvu.SelectedValue.ToString();
                        dgvdanhsachnv.DataSource = STAFFBUS.SelectFollowPB(nv);
                    }
                    if( flag == 1 && this.dgvdanhsachnv.Rows.Count != 0 )
                    {
                        if( mamay == "-1" )
                        {
                            DeleteALLMachine();
                            SetToolbar();
                            ReloadStattus();
                        }
                        else
                        {
                            DeleteOneMachine();
                            ReloadStattus();
                        }
                        RemoveCheckbox();
                        STAFF nv = new STAFF();
                        nv.MAPB = this.cmbphongban.SelectedValue.ToString();
                        nv.MACV = this.cmbchucvu.SelectedValue.ToString();
                        dgvdanhsachnv.DataSource = STAFFBUS.SelectFollowPB(nv);

                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Có lỗi :" + ex.Message.ToString());
            }
        }
        private void Connection(ConnectType type, bool isdomain, MAYCHAMCONG mcc)
        {
            try
            {
                if( type == ConnectType.COM )
                {
                    /// doc toc do may
                    DataTable tocdo = new DataTable();
                    TOCDO td = new TOCDO();
                    td.MATD = mcc.MATD;
                    tocdo = TOCDOBUS.SelectPK(td);
                    int tocdocom = 0;
                    foreach( DataRow rowtocdo in tocdo.Rows )
                    {
                        tocdocom = int.Parse(rowtocdo ["TENTOCDO"].ToString());
                        break;
                    }
                    this.IsConnect = AxCZKEM1.Connect_Com(mcc.COMPORT , mcc.IDMAY , tocdocom);
                }
                else if( type == ConnectType.TCP )
                {
                    if( mcc.ISDOMAIN )
                    {
                        this.IsConnect = AxCZKEM1.Connect_Net(mcc.DOMAIN , int.Parse(mcc.PORT));
                    }
                    else
                    {
                        this.IsConnect = AxCZKEM1.Connect_Net(mcc.IP , int.Parse(mcc.PORT));
                    }
                }
                else if( type == ConnectType.USB )
                {
                    this.IsConnect = AxCZKEM1.Connect_USB(mcc.IDMAY);
                }
            }
            catch( Exception ex )
            {
                MessageBox.Show(ex.Message.ToString());
                SetToolbar();
            }
        }
        private void DataTableToMaychamcong(ref MAYCHAMCONG mcc, DataTable dt)
        {
            try
            {
                if( dt.Rows.Count != 0 )
                {
                    foreach( DataRow row in dt.Rows )
                    {
                        mcc.COMPORT = int.Parse(row ["COMPORT"].ToString());
                        mcc.DOMAIN = row ["DOMAIN"].ToString();
                        mcc.FIMWARE = row ["FIMWARE"].ToString();
                        mcc.IDMAY = int.Parse(row ["IDMAY"].ToString());
                        mcc.IP = row ["IP"].ToString();
                        if( row ["ISDOMAIN"].ToString() == "USE" )
                            mcc.ISDOMAIN = true;
                        else
                            mcc.ISDOMAIN = false;
                        mcc.MALKN = row ["MALKN"].ToString();
                        mcc.MALM = row ["MALM"].ToString();
                        mcc.MAMCC = row ["MAMCC"].ToString();
                        mcc.MATD = row ["MATD"].ToString();
                        mcc.PASSWORD = row ["PASSWORD"].ToString();
                        mcc.PORT = row ["PORT"].ToString();
                        mcc.SeriNumber = row ["SeriNumber"].ToString();
                        mcc.SODANGKI = row ["SODANGKI"].ToString();
                        mcc.TENMAY = row ["TENMAY"].ToString();
                        mcc.TONGSOMATMA = int.Parse(row ["TONGSOMATMA"].ToString());
                        mcc.TONGSONHANVIEN = int.Parse(row ["TONGSONHANVIEN"].ToString());
                        mcc.TONGSOVANTAY = int.Parse(row ["TONGSOVANTAY"].ToString());
                    }
                }
            }
            catch( Exception ex )
            {
                MessageBox.Show("Có lỗi :" + ex.Message.ToString());
            }
           
        }
        private int Tongsomay()
        {
            int somgay = 0;
            DataTable dt = new DataTable();
            dt = MAYCHAMCONGBUS.SumMachineZK();
            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    somgay = int.Parse(dr["tongsomayzk"].ToString());
                    break;
                }
            }
            else
            {
                somgay = 0;
            }
            return somgay;
        }
        private void ConvertData(DataTable dt)
        {
            try
            {
                if( dt.Rows.Count != 0 )
                {
                    dsmaychamcong.Clear();
                    foreach( DataRow row in dt.Rows )
                    {
                        mcc = new MAYCHAMCONG();
                        mcc.COMPORT = int.Parse(row ["COMPORT"].ToString());
                        mcc.DOMAIN = row ["DOMAIN"].ToString();
                        mcc.FIMWARE = row ["FIMWARE"].ToString();
                        mcc.IDMAY = int.Parse(row ["IDMAY"].ToString());
                        mcc.IP = row ["IP"].ToString();
                        if( row ["ISDOMAIN"].ToString() == "USE" )
                            mcc.ISDOMAIN = true;
                        else
                            mcc.ISDOMAIN = false;
                        mcc.MALKN = row ["MALKN"].ToString();
                        mcc.MALM = row ["MALM"].ToString();
                        mcc.MAMCC = row ["MAMCC"].ToString();
                        mcc.MATD = row ["MATD"].ToString();
                        mcc.PASSWORD = row ["PASSWORD"].ToString();
                        mcc.PORT = row ["PORT"].ToString();
                        mcc.SeriNumber = row ["SeriNumber"].ToString();
                        mcc.SODANGKI = row ["SODANGKI"].ToString();
                        mcc.TENMAY = row ["TENMAY"].ToString();
                        mcc.TONGSOMATMA = int.Parse(row ["TONGSOMATMA"].ToString());
                        mcc.TONGSONHANVIEN = int.Parse(row ["TONGSONHANVIEN"].ToString());
                        mcc.TONGSOVANTAY = int.Parse(row ["TONGSOVANTAY"].ToString());
                        dsmaychamcong.Add(mcc);
                    }
                }
            }
            catch( Exception ex )
            {
                MessageBox.Show("Có lỗi :" + ex.Message.ToString());
            }
        }
        private void toolbarMayChamCong1_Delete()
        {

            if (cmbphongban.Enabled == true && cmbchucvu.Enabled == true)
            {
                this.toolbarMayChamCong1.SetEnableButton(false, false, false, false, true, false, false, false, true);
                this.dgvdanhsachnv.ReadOnly = false;
                if (this.cmbphongban.SelectedValue.ToString() != "System.Data.DataRowView" && this.cmbchucvu.SelectedValue.ToString() != "System.Data.DataRowView")
                {
                    STAFF nv = new STAFF();
                    nv.MAPB = this.cmbphongban.SelectedValue.ToString();
                    nv.MACV = this.cmbchucvu.SelectedValue.ToString();
                    AddCheckbox(STAFFBUS.SelectFollowPB(nv));
                    //sau khi chon du lieu 
                    flag = 1;
                }
            }
        }
        private void DeleteOneMachine()
        {
            if (!String.IsNullOrEmpty(mamay))
            {
                if (mamay.Trim() == "-1")
                {
                    thongbao = new frmHoidap("Bạn vui lòng chọn máy cụ thể!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                    thongbao.ShowDialog();
                }
                else
                {
                        DataTable dt = new DataTable();
                        mcc = new MAYCHAMCONG();
                        mcc.MAMCC = mamay;
                        dt = MAYCHAMCONGBUS.SelectPK(mcc);
                        
                        if (dt.Rows.Count != 0)
                        {
                            mcc = new MAYCHAMCONG();
                            DataTableToMaychamcong(ref mcc, dt);
                            frmwaiting f = new frmwaiting();
                            f.StartPosition = FormStartPosition.CenterScreen;
                            f.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                            f.Show();
                            Application.DoEvents();
                            CreateConection(mcc);
                            //neu connect thanh cong 
                            if (IsConnect)
                            {
                                AxCZKEM1.GetSerialNumber(mcc.IDMAY, out seri);
                                if( !CheckSeriAl(seri) )
                                {
                                    f.Close();
                                    AxCZKEM1.Disconnect();
                                }
                                else
                                {
                                    int count = dgvdanhsachnv.Rows.Count;
                                    if (count != 0)//neu bang danh sach nhan vien co du lieu
                                    {
                                        f.tocdo.Maximum = count;
                                        f.tocdo.Visible = true;
                                        f.lbgoi.Text = "0%";
                                        f.lbgoi.Text = "Đang xóa nhân viên từ máy " + mcc.IDMAY.ToString();
                                        Application.DoEvents();
                                        AxCZKEM1.EnableDevice(mcc.IDMAY, false);
                                        int num7 = this.dgvdanhsachnv.Rows.Count - 1;
                                        for (int i = 0; i <= num7; i++)
                                        {
                                            DataGridView dgv = this.dgvdanhsachnv;
                                            if (dgv.Rows[i].Cells[0].Value != null)
                                            {
                                                int MAMCC;
                                                if (dgv.Rows[i].Cells["MACCM"].Value.ToString() == "")
                                                    continue;
                                                else
                                                    MAMCC = Conversions.ToInteger(dgv.Rows[i].Cells["MACCM"].Value);

                                                ///chi can ma so nhan vien 
                                                if (AxCZKEM1.IsTFTMachine(mcc.IDMAY))
                                                {
                                                    if (AxCZKEM1.SSR_DeleteEnrollData(mcc.IDMAY, MAMCC.ToString(), 0))
                                                    {
                                                        sonhanvien++;
                                                    }
                                                }
                                                else
                                                {
                                                    if (AxCZKEM1.DeleteEnrollData(mcc.IDMAY, MAMCC, mcc.IDMAY, 0))
                                                        sonhanvien++;
                                                }
                                                //cuoi vong lap
                                                f.lbgoi.Text = Conversions.ToString((double)(Math.Round((double)(((double)(i + 1)) / ((double)count)), 2) * 100.0)) + "%";
                                                f.tocdo.Value = i + 1;
                                                Application.DoEvents();
                                            }

                                        }
                                        AxCZKEM1.EnableDevice(mcc.IDMAY, true);
                                        AxCZKEM1.Disconnect();
                                        f.Close();
                                        thongbao = new frmHoidap("Đã xóa  " + mcc.IDMAY.ToString() +" "+ sonhanvien.ToString()+" nhân viên! ", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                                        thongbao.ShowDialog();
                                        SetToolbar();
                                    }
                                }
                            }
                            else
                            {
                                f.Close();
                                f.Dispose();
                                SetToolbar();
                                thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                thongbao.ShowDialog();
                            }
                        }
                }
            }
        }
        private void DeleteALLMachine() 
        {
            if (!String.IsNullOrEmpty(mamay))
            {
                try
                {
                    int tongsomay = 0;
                    
                    tongsomay = Tongsomay();
                    DataTable danhsachmay = MAYCHAMCONGBUS.SelectAll(null);
                    ConvertData(danhsachmay);
                    foreach (MAYCHAMCONG cc in dsmaychamcong)
                    {
                        sonhanvien = 0;
                        this.UserCount = 0;
                        this.FPCount = 0;
                        frmwaiting f = new frmwaiting();
                        f.StartPosition = FormStartPosition.CenterScreen;
                        f.lbgoi.Text = "Đang gọi máy " + cc.IDMAY.ToString() + "......";
                        f.Show();
                        Application.DoEvents();
                        //tao ket noi
                        CreateConection(mcc);
                        if (IsConnect)
                        {
                            AxCZKEM1.GetSerialNumber(mcc.IDMAY, out seri);
                            if(!CheckSeriAl(seri))
                            {
                                f.Close();
                                AxCZKEM1.Disconnect();
                            }
                            {
                                int count = dgvdanhsachnv.Rows.Count;
                                if (count != 0)//neu bang danh sach nhan vien co du lieu
                                {
                                    f.tocdo.Maximum = count;
                                    f.tocdo.Visible = true;
                                    f.lbgoi.Text = "0%";
                                    f.lbgoi.Text = "Đang xóa nhân viên từ máy " + mcc.IDMAY.ToString();
                                    Application.DoEvents();
                                    AxCZKEM1.EnableDevice(mcc.IDMAY, false);
                                    int num7 = this.dgvdanhsachnv.Rows.Count - 1;
                                    for (int i = 0; i <= num7; i++)
                                    {
                                        DataGridView dgv = this.dgvdanhsachnv;
                                        if (dgv.Rows[i].Cells[0].Value != null)
                                        {
                                            int MAMCC;
                                            if (dgv.Rows[i].Cells["MACCM"].Value.ToString() == "")
                                                continue;
                                            else
                                                MAMCC = Conversions.ToInteger(dgv.Rows[i].Cells["MACCM"].Value);

                                            ///chi can ma so nhan vien 
                                            if (AxCZKEM1.IsTFTMachine(mcc.IDMAY))
                                            {
                                                if (AxCZKEM1.SSR_DeleteEnrollData(mcc.IDMAY, MAMCC.ToString(), 0))
                                                {
                                                    sonhanvien++;
                                                }
                                            }
                                            else
                                            {
                                                if (AxCZKEM1.DeleteEnrollData(mcc.IDMAY, MAMCC, mcc.IDMAY, 0))
                                                    sonhanvien++;
                                            }
                                            //cuoi vong lap
                                            f.lbgoi.Text = Conversions.ToString((double)(Math.Round((double)(((double)(i + 1)) / ((double)count)), 2) * 100.0)) + "%";
                                            f.tocdo.Value = i + 1;
                                            Application.DoEvents();
                                        }

                                    }
                                    AxCZKEM1.EnableDevice(mcc.IDMAY, true);
                                    AxCZKEM1.Disconnect();
                                    f.Close();
                                    thongbao = new frmHoidap("Đã xóa  " + mcc.IDMAY.ToString() + " " + sonhanvien.ToString() + " nhân viên! ", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                                    thongbao.ShowDialog();
                                    SetToolbar();
                                }
                            }
                        }
                        else
                        {
                            f.Close();
                            f.Dispose();
                            thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            thongbao.ShowDialog();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Có lỗi :"+ex.Message.ToString());
                }
            }
        }
        private void toolbarMayChamCong1_Restart()
        {
            try
            {
                if( !String.IsNullOrEmpty(mamay) )
                {
                    if( mamay.Trim() == "-1" )
                    {
                        thongbao = new frmHoidap("Bạn vui lòng chọn máy cụ thể!" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.canhbao);
                        thongbao.ShowDialog();
                    }
                    else
                    {
                        DataTable dt = new DataTable();
                        mcc = new MAYCHAMCONG();
                        mcc.MAMCC = mamay;
                        dt = MAYCHAMCONGBUS.SelectPK(mcc);
                        if( dt.Rows.Count != 0 )
                        {
                            mcc = new MAYCHAMCONG();
                            DataTableToMaychamcong(ref mcc , dt);
                            frmwaiting f = new frmwaiting();
                            f.StartPosition = FormStartPosition.CenterScreen;
                            f.Show();
                            f.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                            Application.DoEvents();
                            CreateConection(mcc);
                            if( IsConnect )
                            {
                                AxCZKEM1.GetSerialNumber(mcc.IDMAY , out seri);
                                if( !CheckSeriAl(seri) )
                                {
                                    f.Close();
                                    AxCZKEM1.Disconnect();
                                }
                                else
                                {
                                    if( !AxCZKEM1.RestartDevice(mcc.IDMAY) )
                                    {
                                        f.Close();
                                        thongbao = new frmHoidap("Không thể khởi động lại máy " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.baoloi);
                                        thongbao.ShowDialog();
                                    }
                                    else
                                    {
                                        f.Close();
                                        thongbao = new frmHoidap(mcc.IDMAY + "Khởi động lại thành công!" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.baoloi);
                                        thongbao.ShowDialog();
                                        AxCZKEM1.Disconnect();
                                    }
                                }
                            }
                            if( !IsConnect )
                            {
                                f.Close();
                                f.Dispose();
                                SetToolbar();
                                thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.baoloi);
                                thongbao.ShowDialog();
                            }
                        }
                    }
                }
               
            }
            catch( Exception ex )
            {
                MessageBox.Show("Có lỗi :" + ex.Message.ToString());
            }

        }
        private void toolbarMayChamCong1_Clock()
        {
            try
            {
                if( !String.IsNullOrEmpty(mamay) )
                {
                    if( mamay.Trim() == "-1" )
                    {
                        thongbao = new frmHoidap("Bạn vui lòng chọn máy cụ thể!" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.canhbao);
                        thongbao.ShowDialog();
                    }
                    else
                    {
                        DataTable dt = new DataTable();
                        mcc = new MAYCHAMCONG();
                        mcc.MAMCC = mamay;
                        dt = MAYCHAMCONGBUS.SelectPK(mcc);
                        if( dt.Rows.Count != 0 )
                        {
                            mcc = new MAYCHAMCONG();
                            DataTableToMaychamcong(ref mcc , dt);
                            frmwaiting f = new frmwaiting();
                            f.StartPosition = FormStartPosition.CenterScreen;
                            f.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                            f.Show();
                            Application.DoEvents();
                            CreateConection(mcc);
                            if( IsConnect )
                            {
                                AxCZKEM1.GetSerialNumber(mcc.IDMAY , out seri);
                                if( CheckSeriAl(seri) )
                                {
                                    f.Close();
                                    AxCZKEM1.Disconnect();
                                }
                                else
                                {

                                    int num = 0;
                                    int num2 = 0;
                                    int num3 = 0;
                                    int num4 = 0;
                                    int num5 = 0;
                                    int num6 = 0;
                                    if( !AxCZKEM1.GetDeviceTime(mcc.IDMAY , ref num6 , ref num3 , ref num , ref num2 , ref num4 , ref num5) )
                                    {
                                        f.Close();
                                        thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.baoloi);
                                        thongbao.ShowDialog();
                                    }
                                    else
                                    {
                                        f.Close();
                                        f.Dispose();
                                        DateTime dateValue = Conversions.ToDate(Conversions.ToString(num6) + "-" + Conversions.ToString(num3) + "-" + Conversions.ToString(num) + " " + Conversions.ToString(num2) + ":" + Conversions.ToString(num4) + ":" + Conversions.ToString(num5));
                                        string str = Until.LayThu(DateAndTime.Weekday(dateValue , FirstDayOfWeek.Sunday));
                                        thongbao = new frmHoidap(mcc.IDMAY + ":" + str + ", " + Conversions.ToString(dateValue) , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.baoloi);
                                        thongbao.ShowDialog();
                                        AxCZKEM1.Disconnect();
                                    }
                                }
                            }
                            if( !IsConnect )
                            {
                                f.Close();
                                f.Dispose();
                                SetToolbar();
                                thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.baoloi);
                                thongbao.ShowDialog();
                            }

                        }
                    }
                }
            }
            catch( Exception ex )
            {
                MessageBox.Show("Có lỗi :" + ex.Message.ToString());
            }
        }
        private void toolbarMayChamCong1_Syssnc()
        {
            try
            {
                int dwYear = DateAndTime.Year(DateAndTime.Now);
                int dwMonth = DateAndTime.Month(DateAndTime.Now);
                int dwDay = DateAndTime.Day(DateAndTime.Now);
                int dwHour = DateAndTime.Hour(DateAndTime.Now);
                int dwMinute = DateAndTime.Minute(DateAndTime.Now);
                int dwSecond = DateAndTime.Second(DateAndTime.Now);
                if( !String.IsNullOrEmpty(mamay) )
                {
                    if( mamay.Trim() == "-1" )
                    {
                        thongbao = new frmHoidap("Bạn vui lòng chọn máy cụ thể!" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.canhbao);
                        thongbao.ShowDialog();
                    }
                    else
                    {
                        DataTable dt = new DataTable();
                        mcc = new MAYCHAMCONG();
                        mcc.MAMCC = mamay;
                        dt = MAYCHAMCONGBUS.SelectPK(mcc);
                        if( dt.Rows.Count != 0 )
                        {
                            mcc = new MAYCHAMCONG();
                            DataTableToMaychamcong(ref mcc , dt);
                            frmwaiting f = new frmwaiting();
                            f.StartPosition = FormStartPosition.CenterScreen;
                            f.Show();
                            f.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                            Application.DoEvents();
                            CreateConection(mcc);
                            if( IsConnect )
                            {
                                AxCZKEM1.GetSerialNumber(mcc.IDMAY , out seri);
                                if(!CheckSeriAl(seri) )
                                {
                                    f.Close();
                                    AxCZKEM1.Disconnect();
                                }
                                else
                                {
                                    if( !AxCZKEM1.SetDeviceTime2(mcc.IDMAY , dwYear , dwMonth , dwDay , dwHour , dwMinute , dwSecond) )
                                    {
                                        f.Close();
                                        thongbao = new frmHoidap("Không thể đồng bộ thời gian với " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.baoloi);
                                        thongbao.ShowDialog();
                                    }
                                    else
                                    {
                                        f.Close();
                                        thongbao = new frmHoidap("Đồng bộ thời gian với " + mcc.IDMAY.ToString() + "thành công!" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.thongtin);
                                        thongbao.ShowDialog();
                                    }
                                }

                            }
                            if( !IsConnect )
                            {
                                f.Close();
                                f.Dispose();
                                SetToolbar();
                                thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.baoloi);
                                thongbao.ShowDialog();
                            }
                        }
                    }
                }
            }
            catch( Exception ex )
            {

                MessageBox.Show("Có lỗi :" + ex.Message.ToString());
            }
        }
        private void toolbarMayChamCong1_Remove()
        {
            try
            {//xoa nhan vien quan ly
                if( !String.IsNullOrEmpty(mamay) )
                {

                    if( mamay.Trim() == "-1" )
                    {
                        thongbao = new frmHoidap("Bạn vui lòng chọn máy cụ thể!" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.canhbao);
                        thongbao.ShowDialog();
                    }
                    else
                    {
                        DataTable dt = new DataTable();
                        mcc = new MAYCHAMCONG();
                        mcc.MAMCC = mamay;
                        dt = MAYCHAMCONGBUS.SelectPK(mcc);
                        if( dt.Rows.Count != 0 )
                        {
                            mcc = new MAYCHAMCONG();
                            DataTableToMaychamcong(ref mcc , dt);
                            thongbao = new frmHoidap("Bạn có muốn xóa nhân viên quản lý của máy " + mcc.IDMAY.ToString() + "?" , frmHoidap.CO.hien , frmHoidap.KHONG.hien , frmHoidap.THOAT.an , frmHoidap.HINH.canhbao);
                            if( thongbao.ShowDialog() == DialogResult.Yes )
                            {
                                frmwaiting f = new frmwaiting();
                                f.StartPosition = FormStartPosition.CenterScreen;
                                f.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                                f.Show();
                                Application.DoEvents();
                                CreateConection(mcc);
                                //neu connect thanh cong 
                                if( IsConnect )
                                {
                                    AxCZKEM1.GetSerialNumber(mcc.IDMAY , out seri);
                                    if(!CheckSeriAl(seri) )
                                    {
                                        f.Close();
                                        AxCZKEM1.Disconnect();
                                    }
                                    if( IsResignter )
                                    {
                                        if( AxCZKEM1.ClearAdministrators(mcc.IDMAY) )
                                        {
                                            f.Close();
                                            SetToolbar();
                                            thongbao = new frmHoidap("Xóa nhân viên quản lý của máy " + mcc.IDMAY.ToString() + "thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                                            thongbao.ShowDialog();
                                        }
                                        else
                                        {
                                            f.Close();
                                            SetToolbar();
                                            thongbao = new frmHoidap("Không thể xóa nhân viên quản lý của máy " + mcc.IDMAY.ToString() + "!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                            thongbao.ShowDialog();
                                           
                                        }
                                    }
                                }
                                else
                                {
                                    f.Close();
                                    f.Dispose();
                                    SetToolbar();
                                    thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại" , frmHoidap.CO.an , frmHoidap.KHONG.an , frmHoidap.THOAT.an , frmHoidap.HINH.baoloi);
                                    thongbao.ShowDialog();
                                }
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                }
            }
            catch( Exception ex )
            {
                MessageBox.Show("Có lỗi :" + ex.Message.ToString());
            }
        }
        private void toolbarMayChamCong1_Exit()
        {
            if (this.Close != null)
                this.Close();
        }
    }
}
